﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WmWebSystem.Models;
using WmWebSystem.Service;

namespace WmWebSystem.DbHelper
{
    public static class FavouriteHelper
    {
        public static void AddNewFansToFavourites(string pno)
        {
            WmDb mydb = new WmDb();

            DateTime time = DateTime.Now;
            string user = HttpContext.Current.User.Identity.Name;
            
            Favourite favourite = new Favourite();
            favourite.ChildPrpjectNo = pno;
            favourite.UserName = user;
            favourite.AddTime = time;
            try
            {
                mydb.Favourites.Add(favourite);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to Favourites, The ChildPrpjectNo:" + pno;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                WriteLog.WriteSysLog(ex.ToString());
            }


        }

        public static IEnumerable<Favourite> FindAllFavouriteHelper(string name)
        {
            WmDb mydb = new WmDb();
            var favourites = from f in mydb.Favourites
                             where f.UserName == name
                             orderby f.AddTime descending
                             select f;
            return favourites;
        }
    }
}